﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using Reporting;

namespace Presentation.Reports.MonthInvoice {
  /// <summary>
  /// Invoice selection for report form
  /// </summary>
  public partial class MonthInvoiceSelectionForm : Form {

    #region - Constructor -

    /// <summary>
    /// Initializes a new instance of the <see cref="MonthInvoiceSelectionForm"/> class.
    /// </summary>
    public MonthInvoiceSelectionForm( ) {
      InitializeComponent( );
    }

    #endregion

    #region - Events -

    private void monthInvoiceSelectionForm_Load( object sender, EventArgs e ) {
      monthTextBox.Text = DateTime.Now.Month.ToString( CultureInfo.CurrentCulture );
      yearTextBox.Text = DateTime.Now.Year.ToString( CultureInfo.CurrentCulture );
      monthTextBox.SelectAll( );

    }

    private void printButton_Click( object sender, EventArgs e ) {

      if ( ValidateChildren( ) ) {

        BusinessLogic.ReportBll bll = new BusinessLogic.ReportBll( );
        int month = Convert.ToInt32( monthTextBox.Text, CultureInfo.CurrentCulture );
        int year = Convert.ToInt32( yearTextBox.Text, CultureInfo.CurrentCulture );
        InvoiceMonthReportData data = bll.GetMonthInvoiceReportData( month, year );
        if ( data.InvoiceMonth.Count > 0 ) {

          ReportViewForm viewForm = new ReportViewForm( data, "InvoiceMonth", "InvoiceMonthDataSet", "Presentation.Reports.MonthInvoice.InvoiceMonthReport.rdlc" );

          viewForm.MdiParent = this.MdiParent;
          viewForm.WindowState = FormWindowState.Maximized;
          viewForm.Show( );

        } else {
          MessageBox.Show( "Geeg gegevens gevonden.", "Afdruk facturen", MessageBoxButtons.OK, MessageBoxIcon.Information );
        }
        this.Close( );
      }
    }

    private void textBox_TextChanged( object sender, EventArgs e ) {
      extendedErrorProvider.SetError( ( TextBox ) sender, "" );
    }

    private void textBox_MouseDown( object sender, MouseEventArgs e ) {
      ( ( TextBox ) sender ).SelectAll( );
    }

    private void textBox_KeyPress( object sender, KeyPressEventArgs e ) {
      if ( !char.IsNumber( e.KeyChar ) && e.KeyChar != ( char ) Keys.Back
        && e.KeyChar != ( char ) Keys.Enter && e.KeyChar != ( char ) Keys.Tab ) {
        e.Handled = true;
      }

    }

    #endregion

    #region - Validation -

    private void monthTextBox_Validating( object sender, CancelEventArgs e ) {
      extendedErrorProvider.SetError( monthTextBox, "" );

      if ( string.IsNullOrEmpty( monthTextBox.Text ) ) {
        extendedErrorProvider.SetIconAlignment( monthTextBox, ErrorIconAlignment.MiddleLeft );
        extendedErrorProvider.SetError( monthTextBox, "Maand verplicht." );
        e.Cancel = true;
      } else {
        int month = Convert.ToInt32( monthTextBox.Text, CultureInfo.CurrentCulture );
        if ( month < 1 || month > 12 ) {
          extendedErrorProvider.SetIconAlignment( monthTextBox, ErrorIconAlignment.MiddleLeft );
          extendedErrorProvider.SetError( monthTextBox, "Ongeldige waarde." );
          e.Cancel = true;
        }
      }

    }

    private void yearTextBox_Validating( object sender, CancelEventArgs e ) {
      extendedErrorProvider.SetError( yearTextBox, "" );

      if ( string.IsNullOrEmpty( yearTextBox.Text ) ) {
        extendedErrorProvider.SetError( yearTextBox, "Jaar verplicht." );
        e.Cancel = true;
      }
    }

    private void textBox_Validated( object sender, EventArgs e ) {
      extendedErrorProvider.SetError( ( Control ) sender, "" );
    }

    #endregion

  }
}
